/**
 * 版权所有(C)，上海勾芒信息科技，2017，所有权利保留。
 * 
 * 项目名：	cards-web
 * 文件名：	WxComponentAppCodeServiceImpl.java
 * 模块说明：	
 * 修改历史：
 * 2017年5月24日 - Debenson - 创建。
 */
package me.chanjar.weixin.component.api.app.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.gomore.jeecg.common.Assert;
import com.google.common.collect.Maps;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.JsonMapper;
import me.chanjar.weixin.component.api.WxCompServiceDelegate;
import me.chanjar.weixin.component.api.app.WxCompMaCodeService;
import me.chanjar.weixin.component.bean.code.AuditStatusQueryResponse;
import me.chanjar.weixin.component.bean.code.WxAppAudit;
import me.chanjar.weixin.component.bean.code.WxAppCategory;
import me.chanjar.weixin.component.bean.code.WxAppCode;

/**
 * 小程序代码管理
 * 
 * @author Debenson
 * @since 0.1
 */
public class WxCompMaCodeServiceImpl implements WxCompMaCodeService {
  public static final String BASE_URL = "https://api.weixin.qq.com/wxa";

  private final WxCompServiceDelegate delegate;

  public WxCompMaCodeServiceImpl(WxCompServiceDelegate delegate) {
    this.delegate = delegate;
  }

  @Override
  public void commit(WxAppCode code) throws WxErrorException {
    Assert.assertArgumentNotNull(code, "code");

    String url = BASE_URL + "/commit";
    delegate.httpPost(url, JsonMapper.defaultMapper().toJson(code));
  }

  @Override
  public InputStream getQrcode() throws WxErrorException {
    String url = BASE_URL + "/get_qrcode";
    File file = delegate.download(url);
    try {
      return new FileInputStream(file);
    } catch (FileNotFoundException e) {
      throw new WxErrorException(-1, e.getMessage(), e);
    }
  }

  @Override
  public List<WxAppCategory> getCategory() throws WxErrorException {
    String url = BASE_URL + "/get_category";
    CategoryQueryResponse resp = delegate.httpGet(url, "", CategoryQueryResponse.class);
    return resp.getCategoryList();
  }

  @Override
  public List<String> getPage() throws WxErrorException {
    String url = BASE_URL + "/get_page";
    PageQueryResponse resp = delegate.httpGet(url, "", PageQueryResponse.class);
    return resp.getPageList();
  }

  @Override
  public String submitAudit(WxAppAudit... auditItems) throws WxErrorException {
    Assert.assertArgumentNotNull(auditItems, "auditItems");

    String url = BASE_URL + "/submit_audit";
    Map<String, Object> postData = Maps.newHashMap();
    postData.put("item_list", auditItems);
    AuditResponse resp = delegate.httpPost(url, postData, AuditResponse.class);
    return resp.getAuditid();
  }

  @Override
  public AuditStatusQueryResponse getAuditStatus(String auditId) throws WxErrorException {
    Assert.assertArgumentNotEmpty(auditId, "auditId");

    String url = BASE_URL + "/get_auditstatus";
    Map<String, Object> post = Maps.newHashMap();
    post.put("auditid", auditId);
    AuditStatusQueryResponse autiStatus = delegate.httpPost(url, post,
        AuditStatusQueryResponse.class);
    return autiStatus;
  }

  @Override
  public void release() throws WxErrorException {
    String url = BASE_URL + "/release";
    delegate.httpPost(url, "{}");
  }

  static class AuditResponse implements Serializable {
    private static final long serialVersionUID = -8288806745006792170L;

    /** 审核编号 */
    private String auditid;

    public String getAuditid() {
      return auditid;
    }

    public void setAuditid(String auditid) {
      this.auditid = auditid;
    }

  }

  static class CategoryQueryResponse implements Serializable {

    private static final long serialVersionUID = 1852565094510536584L;
    /** 可填选的类目列表 */
    @JsonProperty("category_list")
    private List<WxAppCategory> categoryList;

    public List<WxAppCategory> getCategoryList() {
      return categoryList;
    }

    public void setCategoryList(List<WxAppCategory> categoryList) {
      this.categoryList = categoryList;
    }

  }

  static class PageQueryResponse implements Serializable {

    private static final long serialVersionUID = 1852565094510536584L;
    /** page_list 页面配置列表 */
    @JsonProperty("page_list")
    private List<String> pageList;

    public List<String> getPageList() {
      return pageList;
    }

    public void setPageList(List<String> pageList) {
      this.pageList = pageList;
    }

  }
}
